<?php
session_start();
require_once '../header.php';
function __autoload($className)
	{
    	require_once('../class/' . $className . '.class.php');
	}
session::need_member();
$agent=$_SESSION["paypalAgent"];
$UserId=session::getuserid();
//$CanBuy=user::need_CanBuy($UserId);
$CanBuy=TRUE;
if(empty($UserId))
{
	$CanBuy==FALSE;
}
if($CanBuy==FALSE)
{
	$_SESSION["erro"]="System Erro, plz relogin and buy again";
	redirect::location("../membercp.php");
	unset($_SESSION["cc"]);
	unset($_SESSION["ccid"]);
	unset($_SESSION["moneyplus"]);

}
else 
{
	$paypalId=$_SESSION["ppid"];
	$paypalIdList=array();
	foreach($paypalId as $listPp)
	{
		$paypalIdList[]=$listPp;
	}
	$countPp=count($paypalId);
	$plusmoney=$_SESSION["plusmoney"]*$countPp;
	$moneyHaveToPay=NULL;
	if($agent=="mrhieu")
	{
		$getPrice=buyPaypal::getMrhieuPrice($paypalId);
		$moneyHaveToPay=$plusmoney+$getPrice;
	}
	$checkMoney=user::checkpermission('userinfo',"userid=$UserId", "money >= $moneyHaveToPay");
	if($checkMoney==1)
	{
		$moneyAfter=database::Update("userinfo",array(
		'UserID' => $UserId
		),
		array('money' => array( "money - {$moneyHaveToPay}" ))
		);
		$insertOrder=user::insertorder($UserId, $moneyHaveToPay, 'mrhieu','paypal');
		$updatePaypal=database::Update("paypal",array(
		'id' => $paypalIdList
		),array(
		'is_sold' => 'Y',
		'user_id' => $UserId,
		'order_id' => $insertOrder
		));
		$_SESSION["erro"]="Thank You, Your order has been completed";
		redirect::location("../orderhistory.php");
	}
	else
	{
		$_SESSION["erro"]="You Do not have enough money";
		unset($_SESSION["pp"]);
		redirect::location("../lr/deposit.php");
	}
}
?>